<?php
/*
 * @Author       : chaihao
 * @Date         : 2020-06-01 10:58:11
 * @FilePath     : /ai/controllers/api/UserController.php
 */

namespace app\controllers\api;


use yii;
use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\api\UserService;
use yii\web\Controller;
use linslin\yii2\curl;


class UserController extends Controller
{
    public $req = array();
    public $user = array();

    /**
     * 安全 校验 屏蔽
     * 预处理  权限等等操作处理
     */
    public function beforeAction($action)
    {
        $this->req = Helper::in(array_merge($_POST, $_GET));
        return true;
    }

    /**
     * 登录
     *
     */
    public function actionLogin()
    {
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'username'  => "required",
            'verification_code'  => "required|integer",
        );
        # 请求参数验证
        if (is_numeric($req['username'])) {
            Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
            $req['cellphone'] = $req['username'];
        } else {
            $rules['username'] = "required|email";
            Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
            $req['email'] = $req['username'];
        }
        $res = UserService::getInstance()->login($req);
        Helper::right_out($req, $res);
    }

    public function actionRegister()
    {
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'username'  => "required",
            'nickname'  => "required",
            'name'  => "required",
            'english_name'  => "required",
            'english_company'  => "required",
            'english_position'  => "required",
            'company'  => "required",
            'position'  => "required",
            'verification_code'  => "required|integer",
            // 'id_card'  => "required",
            'message' => 'required',
            'card' => 'required',
        );
        # 请求参数验证
        if (is_numeric($req['username'])) {
            Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
            $req['cellphone'] = $req['username'];
        } else {
            $rules['username'] = "required|email";
            Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
            $req['email'] = $req['username'];
        }
        $res = UserService::getInstance()->register($req);
        Helper::right_out($req, $res);
    }

    public function actionGetUserCountry()
    {
        $req = $this->req;
        # 请求参数验证
        $res = UserService::getInstance()->getUserCountry($req);
        Helper::right_out($req, $res);
    }

    /**
     *  邀请码绑定
     */
    public function actionBindCode()
    {
        $req = $this->req;
        # 请求参数验证
        $rules = array(
            'token'            => "required",
            'ivtcode'          => "required",
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $this->user = Helper::checksign($req);
        $res = UserService::getInstance()->bindCode($req['ivtcode'],$this->user);
        Helper::right_out($req, $res);
    }
}
